package com.hemaapp.yjnh.view.calendar;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

/**个人注解：日历列表中的某个月。
 * 包含一个title显示当前是某年某月，还有一个CalendarGridView用来显示这个月每天的信息
 * @author GuiyouZhang
 * 2015年9月26日  下午11:34:43
 */
public class MonthView extends LinearLayout {
    
	TextView title;
	CalendarGridView grid;//grid代表要显示的一整个月
	private Listener listener;
	private List<CalendarCellDecorator> decorators;
	private int flag=0;

	public static MonthView create(ViewGroup parent, LayoutInflater inflater,
			DateFormat weekdayNameFormat, Listener listener, Listener2 listener2, Calendar today,
			int dividerColor, int dayBackgroundResId, int dayTextColorResId,
			int titleTextColor, boolean displayHeader, int headerTextColor) {
		return create(parent, inflater, weekdayNameFormat, listener,listener2, today,
				dividerColor, dayBackgroundResId, dayTextColorResId,
				titleTextColor, displayHeader, headerTextColor, null);
	}

	public static MonthView create(ViewGroup parent, LayoutInflater inflater,
			DateFormat weekdayNameFormat, Listener listener,Listener2 listener2, Calendar today,
			int dividerColor, int dayBackgroundResId, int dayTextColorResId,
			int titleTextColor, boolean displayHeader, int headerTextColor,
			List<CalendarCellDecorator> decorators) {
		final MonthView view = (MonthView) inflater.inflate(R.layout.month,
				parent, false);
		view.setDividerColor(dividerColor);
		view.setDayTextColor(dayTextColorResId);
		view.setTitleTextColor(titleTextColor);
		view.setDisplayHeader(displayHeader);
		view.setHeaderTextColor(headerTextColor);

		if (dayBackgroundResId != 0) {
			view.setDayBackground(dayBackgroundResId);
		}

		final int originalDayOfWeek = today.get(Calendar.DAY_OF_WEEK);

		int firstDayOfWeek = today.getFirstDayOfWeek();
		final CalendarRowView headerRow = (CalendarRowView) view.grid
				.getChildAt(0);
		for (int offset = 0; offset < 7; offset++) {
			today.set(Calendar.DAY_OF_WEEK, firstDayOfWeek + offset);
			final TextView textView = (TextView) headerRow.getChildAt(offset);
			// 显示的是星期几的形式
			textView.setText(weekdayNameFormat.format(today.getTime()));
			textView.setVisibility(View.GONE);
		}
		today.set(Calendar.DAY_OF_WEEK, originalDayOfWeek);
		view.listener = listener;
		view.decorators = decorators;
		return view;
	}

	public MonthView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void setDecorators(List<CalendarCellDecorator> decorators) {
		this.decorators = decorators;
	}

	public List<CalendarCellDecorator> getDecorators() {
		return decorators;
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		title = (TextView) findViewById(R.id.title);
		grid = (CalendarGridView) findViewById(R.id.calendar_grid);
	}

	public void init(MonthDescriptor month,
			List<List<MonthCellDescriptor>> cells, boolean displayOnly,
			Typeface titleTypeface, Typeface dateTypeface,String refusedates) {
		title.setText(month.getLabel());
		final int numRows = cells.size();
		Log.d("7897897898789-------", numRows+"");
		grid.setNumRows(numRows);
		for (int i = 0; i < 6; i++) {//每个月最多显示6行
			
			CalendarRowView weekRow = (CalendarRowView) grid.getChildAt(i + 1);//一次取出6周和一个header
			weekRow.setListener(listener);

			if (i < numRows) {
				weekRow.setVisibility(VISIBLE);
				List<MonthCellDescriptor> week = cells.get(i);
				//判断这一周有多少天，也就是这一行显示多少天
				for (int c = 0; c < week.size(); c++) {
					MonthCellDescriptor cell = week.get(c);
					//cellView代表显示的每一天
					CalendarCellView cellView = (CalendarCellView) weekRow
							.getChildAt(c);
					String cellDate = Integer.toString(cell.getValue());
					// 为日期数据赋值
					if (!cellView.getText().equals(cellDate)) {
						cellView.setText(cellDate);
					}

					// 判断获得的日期是否属于当前月份，在不属于的情况下，将cellView设置成不可见
					if (cell.isCurrentMonth()) {
						cellView.setEnabled(cell.isCurrentMonth());
					} else {
						cellView.setText("");
						cellView.setVisibility(View.INVISIBLE);
					}
					//张延山优化，避免上下滚动后部分日期不显示start
					if(i==numRows-2)
						cellView.setVisibility(View.VISIBLE);
					if(i==0&&cell.isCurrentMonth())
						cellView.setVisibility(View.VISIBLE);
					//张延山优化，避免上下滚动后部分日期不显示end
//					cellView.setClickable(!displayOnly);
					cellView.setSelectable(cell.isSelectable());
					cellView.setSelected(cell.isSelected());
					cellView.setCurrentMonth(cell.isCurrentMonth());
					cellView.setToday(cell.isToday());
					cellView.setRangeState(cell.getRangeState());
					cellView.setHighlighted(cell.isHighlighted());
					cellView.setTag(cell);
					//今天不被默认选中
//					if(cell.isToday())
//						cellView.setSelected(false);
					//设置显示出不可用日期
					String date = BaseUtil.transString(cell.getDate(), new SimpleDateFormat("yyyy-MM-dd"));
					if(refusedates!=null && refusedates.contains(date)){
						cellView.setClickable(false);
						cellView.setTextColor(getResources().getColor(R.color.calendar_day_unuse_text));
						cellView.setBackgroundResource(R.color.calendar_day_unuse_bg);
					}else {
						cellView.setClickable(!displayOnly);
//						cellView.setTextColor(getResources().getColorStateList(R.drawable.calendar_text_selector));
						cellView.setTextColor(Color.parseColor("#464646"));
						cellView.setBackgroundResource(R.drawable.calendar_bg_selector);
					}
					//日历中某一天的点击事件
//					cellView.setOnClickListener(new OnClickListener() {
//
//						@Override
//						public void onClick(View v) {
//							
//						}
//					});
					if (null != decorators) {
//						if(decorators.size()>1){
//							for(int j = 0; j<decorators.size(); j++){
//								CalendarCellDecorator decorator = decorators.get(j);
//								if(j == 0){
//									decorator.decorate(cellView, cell.getDate(), "1");
//								}else if(j == decorators.size() - 1){
//									decorator.decorate(cellView, cell.getDate(), "2");
//								}else {
//									decorator.decorate(cellView, cell.getDate(), "3");
//								}
//								
//							}
//						}
						for (CalendarCellDecorator decorator : decorators) {
							decorator.decorate(cellView, cell.getDate(),"");
						}
					}
				}
			} else {
				weekRow.setVisibility(GONE);
			}
		}

		if (titleTypeface != null) {
			title.setTypeface(titleTypeface);
		}
		if (dateTypeface != null) {
			grid.setTypeface(dateTypeface);
		}
	}

	public void setDividerColor(int color) {
		grid.setDividerColor(color);
	}

	public void setDayBackground(int resId) {
		grid.setDayBackground(resId);
	}

	public void setDayTextColor(int resId) {
		grid.setDayTextColor(resId);
	}

	public void setTitleTextColor(int color) {
		title.setTextColor(color);
	}

	public void setDisplayHeader(boolean displayHeader) {
		grid.setDisplayHeader(displayHeader);
	}

	public void setHeaderTextColor(int color) {
		grid.setHeaderTextColor(color);
	}

	public interface Listener {
		void handleClick(MonthCellDescriptor cell,CalendarCellView cellview,int position);
	}
	public interface Listener2 {
		void handleClick(CalendarCellView cellview,MonthCellDescriptor cell);
	}
}
